package com.gxa.zhonglong.controller;

import com.gxa.zhonglong.common.JsonResult;
import com.gxa.zhonglong.exception.BusinessException;
import com.gxa.zhonglong.pojo.GoodsPrice;
import com.gxa.zhonglong.pojo.PointsRule;
import com.gxa.zhonglong.service.GoodsPriceService;
import com.gxa.zhonglong.service.GoodsTypeService;
import com.gxa.zhonglong.service.PointsRuleService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * Created with IntelliJ IDEA.
 *
 * @author : 宸濯
 * Date: 2021/10/30 15:37
 * Description:
 * Version: V1.0
 */
@Api(tags = "积分规则api")
@CrossOrigin
@RestController
@RequestMapping("/pointsRule")
public class PointsRuleController {
    @Autowired
    GoodsPriceService goodsPriceService;

    @Autowired
    GoodsTypeService goodsTypeService=null;
    @Autowired
    private PointsRuleService pointsRuleService;

    @PostMapping("/selectByPage")
    @ApiOperation("分页查询积分规则")
    public Map<String,Object> selectByPage(Integer page,Integer size){
        final List<PointsRule> list = pointsRuleService.selectByPage(page, size);
        final JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("1");
        jsonResult.setData(list);
        jsonResult.put("count",pointsRuleService.selectCount());
        return jsonResult.getDataMap();
    }

    @PostMapping("/search")
    @ApiOperation("条件查询")
    @ApiImplicitParams({
            @ApiImplicitParam(value = "商品名称",name = "goodsName"),
            @ApiImplicitParam(value = "销售计价",name = "saleValuation"),
            @ApiImplicitParam(value = "商品类型",name = "goodsType"),
            @ApiImplicitParam(value = "价格类型",name = "priceType")
    })
    public Map<String,Object> search(PointsRule pointsRule){
        final List<Map<String, Object>> search = pointsRuleService.search(pointsRule);
        final JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("1");
        jsonResult.setData(search);
        return jsonResult.getDataMap();
    }

    @PostMapping("/addOrUpdate")
    @ApiOperation("添加/修改积分规则")
    @ApiImplicitParams({
            @ApiImplicitParam(value = "编号id(有是修改,不传是添加)",name = "id"),
            @ApiImplicitParam(value = "商品名称",name = "goodsName"),
            @ApiImplicitParam(value = "销售计价",name = "saleValuation"),
            @ApiImplicitParam(value = "商品类型",name = "goodsType"),
            @ApiImplicitParam(value = "价格类型",name = "priceType"),
            @ApiImplicitParam(value = "积分系数",name = "pointsCoefficient"),
            @ApiImplicitParam(value = "开始时间",name = "startTime"),
            @ApiImplicitParam(value = "结束时间",name = "endTime"),
            @ApiImplicitParam(value = "是否启用",name = "state")
    })
    public Map<String,Object> addOrUpdate(Timestamp startTime,
                                          Timestamp endTime,Long id, String goodsName, String saleValuation,
                                          String goodsType, String priceType, Long pointsCoefficient, Boolean state) throws BusinessException {
        PointsRule pointsRule=new PointsRule(id,goodsName,saleValuation,goodsType,priceType,pointsCoefficient,startTime,endTime,state);
        final JsonResult jsonResult = new JsonResult();
        if (pointsRule.getId()==null) {
            pointsRuleService.add(pointsRule);
            jsonResult.setMsg("添加成功!");
        }else {
            pointsRuleService.update(pointsRule);
            jsonResult.setMsg("修改成功!");
        }
        jsonResult.setCode("1");
        return jsonResult.getDataMap();
    }

    @PostMapping("/del")
    @ApiOperation("删除积分规则")
    public Map<String,Object> del(Long id) throws BusinessException {
        pointsRuleService.del(id);
        final JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("1");
        jsonResult.setMsg("删除成功!");
        return jsonResult.getDataMap();
    }


    @ApiOperation("获得全部商品类型名字")
    @PostMapping("/getNameList")
    public Map getNameList()  {
        List<String> nameList = goodsTypeService.getNameList();
        return new JsonResult("1","成功",nameList).getDataMap();
    }

    @ApiOperation("获得全部商品价格类型")
    @PostMapping("/getGoodsPriceClass")
    public Map getGoodsPriceClass()  {
        List<GoodsPrice> goodsPriceList = goodsPriceService.getGoodsPriceList();
        System.out.println(goodsPriceList);
        List<String> goodsPriceClass=new ArrayList<>();
        for (GoodsPrice goodsPrice : goodsPriceList) {
            String priceClass = goodsPrice.getPriceClass();
            goodsPriceClass.add(priceClass);
        }
        System.out.println(goodsPriceClass);
        return new JsonResult("1","成功",goodsPriceClass).getDataMap();
    }
}
